from UserInterface.InstructionSrc import InstructionSrc
from UserInterface.MyLogger import MyLogger
from UserInterface import UserInterface
from UserInterface.PersistStorage import PersistStorage
from UserInterface.UIException import *

class TextUI(UserInterface):
    def __init__(self, name, instSrc: InstructionSrc, logger: MyLogger, cfg:PersistStorage) -> None:
        super().__init__(instSrc, logger, cfg)
        self.name = name
        self.PrintHello()

    def MainLoop(self) -> None:
        while 1:
            try:
                self.GetInstAndExecute()
            except UIBlankInput as e:
                continue
            except UIEndofInput as e:
                break
            except UIException as e:
                self.logger.print(e)

    def PrintHello(self) -> None:
        titleLen = len(self.name)
        totalLen = titleLen + 10 *2 +2
        titleStr = '#'*10 + f' {self.name} ' + '#'*10 + '\n'
        welcomeStr = '#'*totalLen + '\n' + titleStr + '#'*totalLen + '\n'
        welcomeStr += '\ntips: please input <help> to get instruction information\n'
        self.logger.print(welcomeStr)
